package com.xdcplus.web.service.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.mapper.ToolProductionLineToolMapper;
import com.xdcplus.web.common.pojo.entity.ToolProductionLineTool;
import com.xdcplus.web.common.pojo.dto.ToolProductionLineToolDTO;
import com.xdcplus.web.common.pojo.dto.ToolProductionLineToolFilterDTO;
import com.xdcplus.web.common.pojo.vo.ToolProductionLineToolVO;
import com.xdcplus.web.common.pojo.query.ToolProductionLineToolQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.ToolProductionLineToolService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * (ToolProductionLineTool)表服务实现类
 *
 * @author Man.Chen
 * @since 2024-12-03 16:41:13
 */
@Slf4j
@Service("toolProductionLineToolService")
public class ToolProductionLineToolServiceImpl extends BaseServiceImpl<ToolProductionLineToolMapper, ToolProductionLineTool, ToolProductionLineTool, ToolProductionLineToolVO> implements ToolProductionLineToolService {

    @Autowired
    protected ToolProductionLineToolMapper toolProductionLineToolMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveToolProductionLineTool(ToolProductionLineToolDTO toolProductionLineToolDTO) {

        ToolProductionLineTool toolProductionLineTool = new ToolProductionLineTool();
        BeanUtil.copyProperties(toolProductionLineToolDTO, toolProductionLineTool);
//        toolProductionLineTool.setCreateTime(new Timestamp(DateUtil.current()));
        
        return this.save(toolProductionLineTool);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateToolProductionLineTool(ToolProductionLineToolDTO toolProductionLineToolDTO) {

        ToolProductionLineTool toolProductionLineTool = this.getById(toolProductionLineToolDTO.getId());
        if (ObjectUtil.isNull(toolProductionLineTool)) {
            log.error("updateToolProductionLineTool() The ToolProductionLineTool does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(toolProductionLineToolDTO, toolProductionLineTool);
//        toolProductionLineTool.setUpdatedUser(toolProductionLineToolDTO.getUpdatedUser());
//        toolProductionLineTool.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(toolProductionLineTool);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<ToolProductionLineTool> toolProductionLineToolList) {

        toolProductionLineToolList.forEach(toolProductionLineTool -> {
            ToolProductionLineTool toolProductionLineToolParam = new ToolProductionLineTool();
            toolProductionLineToolParam.setId(toolProductionLineTool.getId());
            if (ObjectUtil.isNotNull(toolProductionLineTool.getId())) {
                toolProductionLineTool.setId(toolProductionLineTool.getId());
//                toolProductionLineTool.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<ToolProductionLineTool> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(ToolProductionLineTool::getId, toolProductionLineTool.getId());
                update(toolProductionLineTool, lambdaUpdate);
            } else {
//                toolProductionLineTool.setCreatedTime(new Timestamp(DateUtil.current()));
                save(toolProductionLineTool);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<ToolProductionLineToolDTO> toolProductionLineToolDTOList) {

        List<ToolProductionLineTool> toolProductionLineToolList = BeanUtils.copyProperties(toolProductionLineToolDTOList, ToolProductionLineTool.class);
        return saveOrUpdateBatch(toolProductionLineToolList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteToolProductionLineToolLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        ToolProductionLineTool toolProductionLineTool = this.getById(id);

        if (ObjectUtil.isNull(toolProductionLineTool)) {
            log.error("deleteToolProductionLineTool() The ToolProductionLineTool does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteToolProductionLineToolByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			ToolProductionLineTool toolProductionLineTool = this.getById(id);
			if (ObjectUtil.isNull(toolProductionLineTool)) {
				log.error("deleteToolProductionLineTool() The ToolProductionLineTool does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<ToolProductionLineTool> queryToolProductionLineToolList(ToolProductionLineToolFilterDTO toolProductionLineToolFilterDTO) {
        ToolProductionLineToolQuery toolProductionLineToolQuery = BeanUtil.copyProperties(toolProductionLineToolFilterDTO, ToolProductionLineToolQuery.class);

        return toolProductionLineToolMapper.queryToolProductionLineTool(toolProductionLineToolQuery);
    }

    @Override
    public List<ToolProductionLineToolVO> queryToolProductionLineToolVOList(ToolProductionLineToolFilterDTO toolProductionLineToolFilterDTO) {
        return this.objectConversion(queryToolProductionLineToolList(toolProductionLineToolFilterDTO));
    }

    @Override
    public PageVO<ToolProductionLineToolVO> queryToolProductionLineTool(ToolProductionLineToolFilterDTO toolProductionLineToolFilterDTO) {
        PageVO<ToolProductionLineToolVO> pageVO = new PageVO<>();

        if (toolProductionLineToolFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(toolProductionLineToolFilterDTO.getCurrentPage(), toolProductionLineToolFilterDTO.getPageSize(),
                    toolProductionLineToolFilterDTO.getOrderType(), toolProductionLineToolFilterDTO.getOrderField());
        }

        List<ToolProductionLineTool> toolProductionLineToolList = queryToolProductionLineToolList(toolProductionLineToolFilterDTO);

        PageInfo<ToolProductionLineTool> pageInfo = new PageInfo<>(toolProductionLineToolList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(toolProductionLineToolList));

        return pageVO;
    }

    @Override
    public ToolProductionLineToolVO queryToolProductionLineToolById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
}
